package web.pre;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Easybuy_product;
import entity.Easybuy_product_category;
import service.EasyBuyProductCategoryService;
import service.EasyBuyProductService;
import service.Impl.EasyBuyProductCategoryServiceImpl;
import service.Impl.EasyBuyProductServiceImpl;
import utils.PageUtils;

/**
 * Servlet implementation class CategoryList
 */
public class CategoryList extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action=request.getParameter("action");
		switch (action) {
		case "list":
			list(request,response);
			break;
		case "search":
			search(request,response);
			break;
		default:
			break;
		}
		
		request.getRequestDispatcher("CategoryList.jsp").forward(request, response);
	}

	private void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("keyWord");
		int currpage=Integer.parseInt(request.getParameter("currpage"));
		int pagesize=4;
		EasyBuyProductService productService=new EasyBuyProductServiceImpl();
		PageUtils<Easybuy_product> list= productService.Search(name, currpage, pagesize);
		
		list.setPageindex(currpage);
		request.setAttribute("utils", list);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id"));
		int type=Integer.parseInt(request.getParameter("type"));
		int currpage=Integer.parseInt(request.getParameter("currpage"));
		int pagesize=4;
		EasyBuyProductCategoryService service=new EasyBuyProductCategoryServiceImpl();
		List<Easybuy_product_category> category=service.list();
		request.getSession().setAttribute("category", category);
		
		EasyBuyProductService productService=new EasyBuyProductServiceImpl();
		PageUtils<Easybuy_product> list =productService.products(id,type,currpage,pagesize);
		
		list.setPageindex(currpage);
		request.setAttribute("utils", list);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
